package logmanager.menu;

import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

//classe de gestion du menu
public class MenuField extends JMenuBar{
	private static final long serialVersionUID = 2188163151279315938L;
	
	private JMenu Edition[];
	private JMenu menu;
	private JMenuBar menuBar;
	private Vector<JMenuItem> cop;
	
	public MenuField(String nameMenu[][]){
		
		cop=new Vector<JMenuItem>();
		int l=nameMenu.length;
		
		Edition=new JMenu[l];
		menuBar=new JMenuBar();
		setMenu(new JMenu());
		for (int i = 0; i < nameMenu.length; i++) 
		{
			int k=0;
			Edition[i]=new JMenu(nameMenu[i][k]);
			for (int j = 1; j < nameMenu[i].length; j++) {				
				if(nameMenu[i][j]=="-"){
					Edition[i].addSeparator();
				}else{
					cop.add(new JMenuItem(nameMenu[i][j]));
					Edition[i].add(cop.lastElement());
				}
			}
			k++;
			add(Edition[i]);
		}
	}
	
	public void addActionListener(ActionListener e){
		for (int i = 0; i < cop.size(); i++) {
			cop.get(i).addActionListener(e);
		}
	}
	
	public void setEnabled(int index, int itemIndex, boolean value){
		Edition[index].getItem(itemIndex).setEnabled(value);
	}
	
	public void setMnemonic(int k,char c){
		Edition[k].setMnemonic(c);
	}
	
	public void setAccelerator(int index,char c){
		switch (c) {
		case 'R':
			cop.get(index).setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_R,
					InputEvent.CTRL_MASK)) ;
			break;
			
		case 'N':
			cop.get(index).setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_N,
					InputEvent.CTRL_MASK)) ;
			break;
		case 'Q':
			cop.get(index).setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_Q,
					InputEvent.CTRL_MASK)) ;
			break;
		case 'F':
			cop.get(index).setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_F,
					InputEvent.CTRL_MASK)) ;
			break;
		default:
			break;
		}		
	}

	public void setMenu(JMenu menu) {
		this.menu = menu;
	}

	public JMenu getMenu() {
		return menu;
	}	
	public JMenuBar getMenuBar() {
		return menuBar;
	}

	public void setMenuBar(JMenuBar menuBar) {
		this.menuBar = menuBar;
	}
}
